﻿namespace Vcare.Controllers
{
  using System;
  using System.Collections.Generic;
  using System.Data;
  using System.Data.Entity;
  using System.Linq;
  using System.Web;
  using System.Web.Mvc;
  using Vcare.Models;
  using Vcare.DAL;
  using Vcare.ViewModels;
  using Vcare.Helpers;
  using System.Web.Security;
  using Vcare.Properties;

    [Authorize]
    public class MembersController : Controller
    {
        private VcareContext db = new VcareContext();

        //
        // GET: /Members/

        public ActionResult Index()
        {
          Guid userID = (Guid)Membership.GetUser().ProviderUserKey;
          UserInfo userInfo = db.UserInfo.Where(m => m.UserID == userID).FirstOrDefault();
          int userLevel = db.Authority.Find(userInfo.AuthorityID).Level;
          ViewBag.Level = userLevel;
          int cc = db.UserInfo.Where(m => m.UserID == userID).FirstOrDefault().CallCentreID ?? 0;
          int companyID=0;
          if (cc!=0)
          {
            companyID = db.CallCentres.Find(cc).CompanyID??0;  
          }
          
          switch (userLevel)
          {
            case 1:
              return View(db.Members
                .Where(m => m.Firstname!=null && m.CompanyID==companyID)
                .OrderByDescending(o => o.ContactDate).ToList());
            case 2:
              return View(db.Members
                .Where(m => m.Firstname != null)
                .Where(m => m.CallCentreID == cc)
                .OrderByDescending(o => o.ContactDate)
                .ToList());
            case 3:
              return View(db.Members
                .Where(m => m.Firstname != null)
                .Where(m => m.CallCentreID == cc && m.Paid == null)
                .OrderByDescending(o => o.ContactDate)
                .ToList());
            case 4:
              return View(db.Members
                .Where(m => m.CallCentreID == cc && m.Paid == null)
                .OrderByDescending(o => o.ContactDate)
                .ToList());
            case 5:
              return View(db.Members
                .Where(m => m.Firstname != null)
                .Where(m => m.CallCentreID == cc && m.QAd == null)
                .OrderByDescending(o => o.ContactDate)
                .ToList());
            default:
              break;
          }
          return View();
        }

        //
        // GET: /Members/Details/5

        public ActionResult Details(int id = 0)
        {
            Members members = db.Members.Find(id);
            if (members == null)
            {
                return HttpNotFound();
            }
            return View(members);
        }

        //
        // GET: /Members/Create

        public ActionResult Create()
        {
          AppLists myLists = new AppLists();
          myLists.PopulateLists();
          ViewBag.Title=new SelectList(db.Salutations,"Salutation","Salutation");
          ViewBag.PaymentType = new SelectList(myLists.ListPaymentType, "PaymentType", "PaymentType");
          ViewBag.PricePlan=new SelectList(myLists.PricePlan, "Value", "Text");
          ViewBag.PaymentOption=new SelectList(myLists.PaymentOption);
          ViewBag.CardType = new SelectList(myLists.CardType, "Value", "Text");
          Members member = new Members();        
          return View(member);
        }

        [HttpPost]
        public ActionResult Create(Members member)
        {
            if (ModelState.IsValid)
            {
              // determine the next index number
              string todaysDate = DateTime.Now.ToString("ddMMyy");
              int nextIndex=1;
              if (db.Members.Where(m => m.MembershipID.Contains(todaysDate)).Count() != 0)
              {
                string memberIndex = db.Members
                            .Where(m => m.MembershipID.Contains(todaysDate))
                            .OrderByDescending(m => m.MembershipID.Substring(m.MembershipID.Length - 3))
                            .FirstOrDefault()
                            .MembershipID ;
                nextIndex = Convert.ToInt16(memberIndex.Substring(memberIndex.Length-3)) + 1;
              }
              AppLists myLists = new AppLists();
              myLists.PopulateLists();
              member.ValueOfSinglePayment = Convert.ToDecimal(member.PricePlan);
              member.PricePlan = myLists.PricePlan.Where(m => m.Value == member.PricePlan).FirstOrDefault().Text;
              Guid newID = (Guid)Membership.GetUser().ProviderUserKey;
              int? callCentreid = db.CallCentres.Find(db.UserInfo.Where(m => m.UserID==newID).FirstOrDefault().CallCentreID).CallCentreID;
              member.MembershipID = db.CallCentres.Find(callCentreid).ccsuffix.Trim() + DateTime.Now.ToString("ddMMyy") + nextIndex.ToString().PadLeft(3, '0');
              member.CallCentreID = callCentreid;
              db.Members.Add(member);
              db.SaveChanges();
              return RedirectToAction("Index");
            }

            return RedirectToAction("Index");
        }

        //
        // GET: /Members/Edit/5

        public ActionResult Edit(int id = 0)
        {
            Members members = db.Members.Find(id);
            if (members.CompanyID == null)
            {
              AppLists myLists = new AppLists();
              myLists.PopulateLists();
              ViewBag.PaymentType = new SelectList(myLists.ListPaymentType, "PaymentType", "PaymentType");
              ViewBag.PricePlan = new SelectList(myLists.PricePlan, "Value", "Text");
              ViewBag.PaymentOption = new SelectList(myLists.PaymentOption);
              ViewBag.CardType = new SelectList(myLists.CardType, "Value", "Text");
            }
            
            ViewBag.Titles = new SelectList(db.Salutations, "Salutation", "Salutation");
            
            if (members == null)
            {
                return HttpNotFound();
            }
            return View(members);
        }


        [HttpPost]
        public ActionResult Edit(Members member)
        {
            if (ModelState.IsValid)
            {
              if (member.CompanyID == null)
              {
                AppLists myLists = new AppLists();
                myLists.PopulateLists();
                member.ValueOfSinglePayment = Convert.ToDecimal(member.PricePlan);
                member.PricePlan = myLists.PricePlan.Where(m => m.Value == member.PricePlan).FirstOrDefault().Text;
              }
                db.Entry(member).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(member);
        }

        //
        // GET: /Members/Delete/5

        public ActionResult Delete(int id = 0)
        {
            Members members = db.Members.Find(id);
            if (members == null)
            {
                return HttpNotFound();
            }
            return View(members);
        }

        //
        // POST: /Members/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            Members members = db.Members.Find(id);
            db.Members.Remove(members);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        public ActionResult Checkout(int id, decimal payment)
        {
          Members member = db.Members.Find(id);
          ViewBag.Fullname = string.Format("{0} {1}", db.Members.Find(id).Firstname, db.Members.Find(id).Lastname);
          Session["id"] = id;
          return View(member);
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}